//
//  ActivitySearchVC.m
//  SelfDrivingTravelling-IOS
//
//  Created by Teplot_01 on 16/9/28.
//  Copyright © 2016年 Teplot_01. All rights reserved.
//

#import "ActivitySearchVC.h"
#import "activityCell.h"
#import "ActivityDetailVC.h"
@interface ActivitySearchVC ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation ActivitySearchVC
{
    UITableView *_tableView;
    
    NSDictionary *_responseDic;
    NSArray *_responseArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"活动搜索";
    self.view.backgroundColor = White_Color;
    
    [self requsetForSearchActityList];
    [self initTableViewForSearchActiList];
    //去除多余的分割线
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}
- (void)requsetForSearchActityList{
    //参数
    NSDictionary *parameters = @{
                                @"seachContent":self.searchKey,
                                @"pageNo": @"1",
                                @"pageSize": @"10",
                                 };
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://115.28.152.130/tour/v1/activity/seach.action" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
        _responseDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@<<<<>>>>",_responseDic);
        _responseArr = _responseDic[@"list"];
        NSString *error = _responseDic[@"code"];
        if (![_responseDic[@"code"] isEqualToString:@"0000"]) {
            [SVProgressHUD showErrorWithStatus:[NSString errorStringWithCode:error]];
            
        }else{
            //请求成功
//            [SVProgressHUD showSuccessWithStatus:@"请求活动列表成功"];
            [_tableView reloadData];
        }
        
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"请求活动列表失败"];
    }];
}
- (void)initTableViewForSearchActiList{
    _tableView = [[UITableView alloc]initWithFrame:self.view.frame];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
}
#pragma - mrak UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _responseArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    activityCell *cell = [[NSBundle mainBundle]loadNibNamed:@"activityCell" owner:nil options:nil][0];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.city.text = _responseDic[@"list"][indexPath.row][@"destination"];
    cell.title.text = _responseDic[@"list"][indexPath.row][@"title"];
    NSString *beginTimeStr = _responseDic[@"list"][indexPath.row][@"stratTime"];
    beginTimeStr = [beginTimeStr substringToIndex:10];
    cell.time.text = beginTimeStr;
    NSString *urlStr = _responseDic[@"list"][indexPath.row][@"coverImgURL"];
    [cell.corveImage sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"france"]];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ActivityDetailVC *vc = [[ActivityDetailVC alloc]init];
    vc.status = _responseDic[@"list"][indexPath.row][@"status"];
    vc.activityId = _responseDic[@"list"][indexPath.row][@"id"];
    vc.activityHtmlUrlStr = _responseDic[@"list"][indexPath.row][@"htmlUrl"];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
